package com.shitou.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.shitou.pojo.Assignment;
import com.shitou.pojo.News;
import com.shitou.pojo.User;
import com.shitou.service.AssignmentService;
import com.shitou.service.NewsService;
import com.shitou.util.AssignmentUtil;

@Results({@Result(name=UserAction.LOGIN,location="user.jspx",type="redirectAction"),
	@Result(name=BaseAction.RELOAD,location="index.jspx",type="redirectAction")})
public class IndexAction extends SimpleBaseAction{

	private static final long serialVersionUID = 1L;
	
	@Resource
	private NewsService newsService;
	@Resource
	private AssignmentService assignmentService;
	private List<News> newsList;
	private List<Assignment> assignmentList;
	private List<Assignment> userAssignmentList;
	private News news;

	public String execute() throws Exception{
		User u = (User) getSessionAttribute("user");
		if(u == null) {
			putErrorMessage("请您先登录系统");
			return LOGIN;
		} else {
			newsList = newsService.findLastTen();
			assignmentList = assignmentService.findByState(AssignmentUtil.SOLVING);
			userAssignmentList = assignmentService.findByUser(u);
			return SUCCESS;
		}
	}
	
	public String save() throws Exception{
		newsService.save(news, (User)getSessionAttribute("user"));
		return RELOAD;
	}

	//getter and setter
	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public void setAssignmentList(List<Assignment> assignmentList) {
		this.assignmentList = assignmentList;
	}

	public List<Assignment> getAssignmentList() {
		return assignmentList;
	}
	
	public int getSize() {
		return userAssignmentList.size();
	}

	public void setUserAssignmentList(List<Assignment> userAssignmentList) {
		this.userAssignmentList = userAssignmentList;
	}

	public List<Assignment> getUserAssignmentList() {
		return userAssignmentList;
	}
}
